package com.hkts.beltweigher.salve.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hkts.beltweigher.salve.entity.TemperatureRegulationItem;
import com.hkts.beltweigher.salve.mapper.TemperatureRegulationItemMapper;
import com.hkts.beltweigher.salve.service.ITemperatureRegulationItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.beltweigher.utils.redis.RedisConstant;
import com.hkts.beltweigher.utils.redis.RedisUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hml
 * @since 2024-11-19
 */
@Service("temperatureRegulationItemService")
@Transactional
public class TemperatureRegulationItemServiceImpl extends ServiceImpl<TemperatureRegulationItemMapper, TemperatureRegulationItem> implements ITemperatureRegulationItemService {


    @Autowired
    private TemperatureRegulationItemMapper temperatureRegulationItemMapper;

    @Override
    public IPage<TemperatureRegulationItem> selectPage(Page<TemperatureRegulationItem> page, TemperatureRegulationItem temperatureRegulationItem){
        QueryWrapper<TemperatureRegulationItem> query=Wrappers.query();
        return temperatureRegulationItemMapper.selectPage(page,query);
        }


    @Override
    public List<TemperatureRegulationItem> selectList(TemperatureRegulationItem temperatureRegulationItem){
        QueryWrapper<TemperatureRegulationItem> query=Wrappers.query();
        return temperatureRegulationItemMapper.selectList(query);
        }

    @Override
    public List<TemperatureRegulationItem> selectListByOpcId(String opcId) {
        Gson gson = new Gson();
        Object o = RedisUtils.get(RedisConstant.OPCID + opcId);
        if(o==null){
            QueryWrapper<TemperatureRegulationItem> query = Wrappers.query();
            query.lambda().eq(TemperatureRegulationItem::getOpcId,opcId);
            query.lambda().orderByAsc(TemperatureRegulationItem::getFieldType,TemperatureRegulationItem::getFieldOrder);
            List<TemperatureRegulationItem> temperatureRegulationItems = temperatureRegulationItemMapper.selectList(query);
            String s = gson.toJson(temperatureRegulationItems);
            RedisUtils.set(RedisConstant.OPCID+String.valueOf(opcId),s,2, TimeUnit.HOURS);
            return temperatureRegulationItems;
        }else{
            List<TemperatureRegulationItem> temperatureRegulationItems= gson.fromJson(o.toString(), new TypeToken<List<TemperatureRegulationItem>>() {
            }.getType());
            return temperatureRegulationItems;
        }
    }
}
